<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['project:FinPackage:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="FinPackageList" @selection-change="handleSelectionChange">
      <el-table-column label="ID" align="center" prop="id" width="80px"/>
      <el-table-column label="启用时间" align="center" prop="startTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="会计准则" align="center" prop="ruleType" >
        <template #default="scope">
          <FinRuleType :rule-type-val="scope.row.ruleType" ></FinRuleType>
        </template>
      </el-table-column>
      <el-table-column label="纳税性质" align="center" prop="taxType" >
        <template #default="scope">
          <FinTaxType :tax-type-val="scope.row.taxType" ></FinTaxType>
        </template>
      </el-table-column>
      <el-table-column label="凭证审核" align="center" prop="hasCheckVoucher" >
        <template #default="scope">
          <el-switch :model-value="scope.row.hasCheckVoucher" :disabled></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['project:FinPackage:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['project:FinPackage:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改账套对话框 -->
    <el-dialog :title="title" v-model="open" width="80%" append-to-body>
      <el-form ref="FinPackageRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="启用时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="YYYY-MM-DD HH:mm"
            placeholder="请选择启用时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="会计准则" prop="ruleType">
              <el-select v-model="form.ruleType"  clearable >
                <el-option  v-for="item in ruleTypeData" :key="item.code" :label="item.name" :value="item.code"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="增值税"  prop="taxType">
              <el-select v-model="form.taxType"  clearable >
                <el-option  v-for="item in taxTypeData" :key="item.code" :label="item.name" :value="item.code"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="FinPackage">
import { listFinPackage, getFinPackage, delFinPackage, addFinPackage, updateFinPackage } from "@/api/project/FinPackage";
import useUserStore from "@/store/modules/user.js";
import FinRuleType from "@/components/enumShow/FinRuleType.vue";
import FinTaxType from "@/components/enumShow/FinTaxType.vue";

const { proxy } = getCurrentInstance();

const FinPackageList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const ruleTypeData = ref(useUserStore().enumData.FinRuleType);
const taxTypeData = ref(useUserStore().enumData.FinTaxType);


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: null,
    name: null,
    status: null,
    ruleType: null,
    taxType: null,
    hasCheckVoucher: null,
    createId: null,
    updateId: null,
  },
  rules: {
    startTime: [
      { required: true, message: "启用时间不能为空", trigger: "blur" }
    ],
    ruleType: [
      { required: true, message: "会计准则不能为空", trigger: "change" }
    ],
    taxType: [
      { required: true, message: "纳税性质不能为空", trigger: "change" }
    ],
    name: [
      { required: true, message: "名称不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询账套列表 */
function getList() {
  loading.value = true;
  listFinPackage(queryParams.value).then(response => {
    FinPackageList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userId: null,
    startTime: null,
    name: null,
    status: null,
    ruleType: null,
    taxType: null,
    hasCheckVoucher: null,
    createId: null,
    createBy: null,
    createTime: null,
    updateId: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("FinPackageRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加账套";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getFinPackage(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改账套";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["FinPackageRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateFinPackage(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addFinPackage(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除账套编号为"' + _ids + '"的数据项？').then(function() {
    return delFinPackage(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('project/FinPackage/export', {
    ...queryParams.value
  }, `FinPackage_${new Date().getTime()}.xlsx`)
}

getList();
</script>
